<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class resource extends Admin_Controller {

    var $theme_data = array(
        'layout_main' => 'layout',
        'left' => 'include/leftcp',
    );

    function __construct() {
        parent::__construct();
        $this->load->model("muser");
        $this->load->model("mresource");
        $this->url_index = admin_url() . "resource";
        $this->url_edit = $this->url_index . "/edit/";
        $this->url_delete = $this->url_index . "/delete/";
        $this->url_add = $this->url_index . "/edit/0";
    }

    public function index() {
		if (!$this->permission->check("select", $this->mresource->resCode))
			return $this->nopermission();
        $data = array();
        $data['datas'] = $this->mresource->gets();
        $this->theme_data["main"] = "resource/index";
        $this->template->viewAdmin($this->theme_data, $data);
    }

    function edit($resID = 0) {
		if (!$this->permission->check("update", $this->mresource->resCode))
			return $this->nopermission();
        $data = array();
        $post = $this->input->post();
        $data['res'] = $this->mresource->get($resID, 'RES_ID');

        $data['error'] = array('err_code' => 0, 'msg' => '');
        if ($post) {
            if ($resID > 0) {
                $this->mresource->update($resID, 'RES_ID', $post['res']);
            }
			else{
				if (!$this->permission->check("insert", $this->mresource->resCode))
					return $this->nopermission();
				$resID = $this->mresource->insert($post['res']);
			}
            redirect($this->url_edit . $resID);
        }

        $this->theme_data["main"] = "resource/edit";
        $this->template->viewAdmin($this->theme_data, $data);
    }
	
	function delete($resID = 0){
		if (!$this->permission->check("delete", $this->mresource->resCode))
			return '';
		if($resID > 0){
			$this->mresource->delete($resID, 'RES_ID');
		}
	}
	
	function install_default(){
		$arr_default =array();
		$arr_default[] = array(
						'RES_CODE'=>'RESOURCE',
						'RES_NAME'=>'Tài nguyên',
						'RES_LINK'=>'resource',
						'RES_ICON'=>'icon-globe');
		$arr_default[] = array(
						'RES_CODE'=>'USER',
						'RES_NAME'=>'Nhân viên',
						'RES_LINK'=>'user',
						'RES_ICON'=>'icon-user');
		
		$arr_default[] = array(
						'RES_CODE'=>'OS',
						'RES_NAME'=>'Hệ điều hành',
						'RES_LINK'=>'os',
						'RES_ICON'=>'icon-asterisk');
		
		$arr_default[] = array(
						'RES_CODE'=>'SOFT',
						'RES_NAME'=>'Chương trình',
						'RES_LINK'=>'soft',
						'RES_ICON'=>'icon-asterisk');
		
			$arr_default[] = array(
						'RES_CODE'=>'MANUFACTURER',
						'RES_NAME'=>'Nhà sản xuất',
						'RES_LINK'=>'manufacturer',
						'RES_ICON'=>'icon-asterisk');
						
		$arr_default[] = array(
						'RES_CODE'=>'LICENSE',
						'RES_NAME'=>'Bản quyền',
						'RES_LINK'=>'license',
						'RES_ICON'=>'icon-asterisk');
						
		$arr_default[] = array(
						'RES_CODE'=>'CATEGORY',
						'RES_NAME'=>'Danh mục',
						'RES_LINK'=>'category',
						'RES_ICON'=>'icon-asterisk');
		
		
		$arr_default[] = array(
						'RES_CODE'=>'ADS',
						'RES_NAME'=>'Quảng cáo',
						'RES_LINK'=>'ads',
						'RES_ICON'=>'icon-magnet');
		
		$arr_default[] = array(
						'RES_CODE'=>'CONFIG',
						'RES_NAME'=>'Cấu hình',
						'RES_LINK'=>'config',
						'RES_ICON'=>'icon-cog');
		$arr_default[] = array(
						'RES_CODE'=>'SEOPAGE',
						'RES_NAME'=>'SEO page',
						'RES_LINK'=>'seopage',
						'RES_ICON'=>'icon-play-circle');
		foreach($arr_default as $r){
			$this->mresource->insert($r);
			echo "insert into ".$r['RES_CODE']." !!<br>";
		}
		
	}
}
